package com.znsb.msfq.activity;

import android.graphics.Color;
import android.os.Bundle;
import android.text.Spannable;
import android.text.SpannableStringBuilder;
import android.text.TextUtils;
import android.text.style.ForegroundColorSpan;
import android.view.View;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.baidu.mobstat.StatService;
import com.znsb.msfq.BaseActivity;
import com.znsb.msfq.R;
import com.znsb.msfq.app.ZnsbApp;
import com.znsb.msfq.utils.ActivityUtil;
import com.znsb.msfq.utils.ErrorCodeUtils;
import com.znsb.msfq.utils.HttpUtils;
import com.znsb.msfq.utils.JsonResult;
import com.znsb.msfq.utils.LogUtils;
import com.znsb.msfq.utils.OverallData;
import com.znsb.msfq.utils.ResponseUtils;
import com.znsb.msfq.utils.ToastUtils;
import com.znsb.msfq.utils.UrlUtils;
import com.znsb.msfq.utils.ValidateUtils;
import com.znsb.msfq.view.MyTBView;

import org.json.JSONException;
import org.json.JSONObject;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;
import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.Response;

/**
 * @name：
 * @author： 孙科技
 * @phone： 13308334117
 * @createTime： 2017/2/10
 * @modifyTime： 2017/2/10
 * @explain：
 */

public class RegisterActivity extends BaseActivity {
    @Bind(R.id.bar_img_back)
    ImageView barImgBack;
    @Bind(R.id.bar_tv_title)
    TextView barTvTitle;
    @Bind(R.id.bar_tv_other)
    TextView barTvOther;
    @Bind(R.id.register_tv_phone)
    TextView registerTvPhone;
    @Bind(R.id.register_edit_phone)
    EditText registerEditPhone;
    @Bind(R.id.register_button_timer)
    MyTBView registerButtonTimer;
    @Bind(R.id.register_tv_code)
    TextView registerTvCode;
    @Bind(R.id.register_edit_code)
    EditText registerEditCode;
    @Bind(R.id.register_tv_pwd)
    TextView registerTvPwd;
    @Bind(R.id.register_edit_pwd)
    EditText registerEditPwd;
    @Bind(R.id.register_tv_agpwd)
    TextView registerTvAgpwd;
    @Bind(R.id.register_edit_agpwd)
    EditText registerEditAgpwd;
    @Bind(R.id.register_tv_btn)
    TextView registerTvBtn;
    @Bind(R.id.register_checkBox)
    CheckBox registerCheckBox;
    @Bind(R.id.register_tv_pro)
    TextView registerTvPro;

    @Override
    protected int getlayoutView() {
        return R.layout.act_register;
    }

    @Override
    protected void initView(View view) {
        barTvTitle.setText("注册");
        String tempStr = "《马上分期用户协议》";
        SpannableStringBuilder style = new SpannableStringBuilder(tempStr);
        style.setSpan(new ForegroundColorSpan(Color.rgb(153, 153, 153)), 0, 1, Spannable.SPAN_EXCLUSIVE_INCLUSIVE);
        style.setSpan(new ForegroundColorSpan(Color.rgb(255, 64, 80)), 1, tempStr.length() - 1,
                Spannable.SPAN_EXCLUSIVE_INCLUSIVE);// 设置指定位置文字的颜色5
        registerTvPro.setText(style);
        registerCheckBox.setChecked(true);
    }

    @Override
    protected void initData() {

    }

    @Override
    protected void clickListener(View view) {

    }

    @OnClick({R.id.bar_img_back, R.id.register_button_timer, R.id.register_tv_btn, R.id.register_tv_pro})
    public void onClick(View view) {
        String phone = registerEditPhone.getText().toString().trim();
        switch (view.getId()) {
            case R.id.bar_img_back:
                finish();
                break;
            case R.id.register_button_timer:
                if (ValidateUtils.isMobile(phone)) {
                    getRegisterCode(phone);
                    registerButtonTimer.start();
                }
                break;
            case R.id.register_tv_btn:

                String code = registerEditCode.getText().toString().trim();
                String pwd = registerEditPwd.getText().toString().trim();
                String agpwd = registerEditAgpwd.getText().toString().trim();
                boolean isChecked = registerCheckBox.isChecked();
                if (ValidateUtils.isRegister(phone, code, pwd, agpwd, isChecked))
                    setRegister(phone, code, pwd, agpwd);
                break;
            case R.id.register_tv_pro:
                //用户协议界面
                String url = "file:///android_asset/reg.html";
                Bundle bundle = new Bundle();
                bundle.putString("title", "用户协议");
                bundle.putString("url", url);
                ActivityUtil.next(RegisterActivity.this, WebViewActivity.class, bundle);
                break;
        }
    }


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // TODO: add setContentView(...) invocation
        ButterKnife.bind(this);
    }

    /**
     * 获取注册验证码
     *
     * @param phone
     */
    private void getRegisterCode(String phone) {
        Map<String, Object> maps = new HashMap<>();
        maps.put("phone", phone);
        HttpUtils.getInstance(this).postAsnyRequest(UrlUtils.MSFQ_URL_REGISTERCODE, HttpUtils.getMaps(maps),
                new ResponseUtils() {
                    @Override
                    public void onSucceed(String code, JSONObject request) {
                        LogUtils.i("TAG:", "register_code=" + request);
                        if (ErrorCodeUtils.code6.equals(code)) {
                            registerButtonTimer.stop();
                            ToastUtils.TextToast(ZnsbApp.mContext, ErrorCodeUtils.getCode(code), 2000);
                        }
                    }

                    @Override
                    public void onFailure(String result) {
                    }
                });
    }

    /**
     * 注册
     */
    private void setRegister(String phone, String code, String pwd, String agPwd) {
        Map<String, Object> maps = new HashMap<>();
        maps.put("username", phone);
        maps.put("registerCode", code);
        maps.put("password", pwd);
        maps.put("affirmPassword", agPwd);
        maps.put("type", 0);
        HttpUtils.getInstance(this).postAsnyRequest(UrlUtils.MSFQ_URL_REGISTER, HttpUtils.getMaps(maps),
                new ResponseUtils() {
                    @Override
                    public void onSucceed(String code, JSONObject request) {
                        LogUtils.i("TAG:", "register=" + request);
                        StatService.onEvent(RegisterActivity.this, "zhuce", "pass", 1);//百度统计 注册自定义事件
                        ToastUtils.TextToast(ZnsbApp.mContext, "注册成功", 2000);
                        finish();
                    }

                    @Override
                    public void onFailure(String result) {
                    }
                });
    }

    @Override
    protected void onPause() {
        super.onPause();
//        registerButtonTimer.stop();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
//        registerButtonTimer.stop();
    }
}
